<?php

namespace app\api\controller;

use app\common\exception\BaseException;
use app\common\library\wechat\applet\UrlLink;

class Applet extends Controller
{
    public function generateUrlLink()
    {
        $uid = $this->request->param('uid');
        if (empty($uid)) {
            throw new BaseException(['msg' => '缺少必要的参数：uid']);
        }
        $path = 'pages/index/index';
        return UrlLink::instance($this->wxapp_id)->generate($path, 'scene=uid:' . $uid);
    }
    
    public function openApplet()
    {
        $uid = $this->request->param('uid');
        if (empty($uid)) {
            throw new BaseException(['msg' => '缺少必要的参数：uid']);
        }
        $path = 'pages/index/index';
        $result = UrlLink::instance($this->wxapp_id)->generate($path, 'scene=uid:' . $uid);
        //重定向浏览器
        header("Location: {$result['url_link']}");
        //确保重定向后，后续代码不会被执行
        exit;
    }
}